'use strict';

const { Controller } = require('egg');

class CoursesController extends Controller {

    async list() {
        const { ctx, app } = this;
        const pageSize = ctx.query.pageSize ? parseInt(ctx.query.pageSize) : 10;
        const pageNumber = ctx.query.pageNumber ? parseInt(ctx.query.pageNumber) : 1;
        let where = {};
        let { count, rows } = await app.model.Courses.findAndCountAll({
            where: where,
            offset: (pageNumber - 1) * pageSize,
            limit: pageSize,
        });
        // 返回用户信息和token
        ctx.apiSuccess({
            totalCount: count,
            currPage: pageNumber,
            list: rows
        });
    };


    async addCourses() {
        const { ctx, app } = this;
        ctx.validate({
            coursescoverimg: {
                type: 'string',
                required: true,
                desc: "图片路径"
            },
            coursesdetailcontent: {
                type: 'string',
                desc: '详细',
                default: ''
            },
            coursesname: {
                type: 'string',
                required: true,
                desc: '课程名称',
                default: ''
            },
            coursessellstatus: {
                type: 'int',
                required: true,
                desc: '状态',
                range: { in: [0, 1]
                }
            },
            originalprice: {
                type: 'int',
                required: true,
                desc: '课程价格',
            },
            sellingprice: {
                type: 'int',
                required: true,
                desc: '课程价格',
            },
            coursestype: {
                type: 'int',
                required: true,
                desc: '课程类型',
                range: { in: [1, 2, 3, 4, 5, 6, 7, 8, 9]
                }
            },
            coursesimg: {
                type: 'string',
                required: true,
                desc: "图片路径"
            },
            coursessellstatus: {
                type: 'int',
                required: true,
                desc: '推荐',
                range: { in: [0, 1]
                }
            },
            courseslecturer: {
                type: 'string',
                required: true,
                desc: '讲师',
            }
        });
        const { coursescoverimg, coursesdetailcontent, coursesname, coursessellstatus, originalprice, sellingprice, coursestype, coursesimg, coursesrecommend, courseslecturer } = ctx.request.body;
        await app.model.Courses.create({
            coursescoverimg,
            coursesdetailcontent,
            coursesname,
            coursessellstatus,
            originalprice,
            sellingprice,
            coursestype,
            coursesimg,
            coursesrecommend,
            courseslecturer
        });
        ctx.apiSuccess('添加成功')
    };

    async updateCourses() {
        const { ctx, app } = this;
        ctx.validate({
            id: {
                type: 'int',
                required: true,
                desc: '轮播图id',
            },
            coursescoverimg: {
                type: 'string',
                required: true,
                desc: "图片路径"
            },
            coursesdetailcontent: {
                type: 'string',
                desc: '详细',
                default: ''
            },
            coursesname: {
                type: 'string',
                required: true,
                desc: '课程名称',
                default: ''
            },
            coursessellstatus: {
                type: 'int',
                required: true,
                desc: '状态',
                range: { in: [0, 1]
                }
            },
            originalprice: {
                type: 'int',
                required: true,
                desc: '课程价格',
            },
            sellingprice: {
                type: 'int',
                required: true,
                desc: '课程价格',
            },
            coursestype: {
                type: 'int',
                required: true,
                desc: '课程类型',
                range: { in: [1, 2, 3, 4, 5, 6, 7, 8, 9]
                }
            },
            coursesimg: {
                type: 'string',
                required: true,
                desc: "图片路径"
            },
            coursesrecommend: {
                type: 'int',
                required: true,
                desc: '推荐',
                range: { in: [0, 1]
                }
            },
            courseslecturer: {
                type: 'string',
                required: true,
                desc: '讲师',
            }
        });
        const { coursescoverimg, coursesdetailcontent, coursesname, coursessellstatus, originalprice, sellingprice, coursestype, coursesimg, coursesrecommend, courseslecturer, id } = ctx.request.body;

        let courses = await app.model.Courses.findOne({ id });
        if (!courses) {
            ctx.throw('更新失败,不存在此课程')
        };
        // swiper.image = image;
        // swiper.link = link;
        // swiper.order = order;
        // await swiper.save();
        await app.model.Courses.update({ coursescoverimg, coursesdetailcontent, coursesname, coursessellstatus, originalprice, sellingprice, coursestype, coursesimg, coursesrecommend, courseslecturer }, {
            where: {
                id
            }
        })
        ctx.apiSuccess('更新成功');
    }

    async deleteCourses() {
        const { ctx, app } = this;
        ctx.validate({
            ids: {
                type: 'all',
                desc: '课程ids'
            }
        });
        let { ids } = ctx.request.body;
        ids = JSON.parse(ids);
        await app.model.Courses.destroy({
            where: {
                id: ids
            }
        });
        ctx.apiSuccess('删除成功');
    }

    async detail() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        let courses = await app.model.Courses.findOne({
            where: {
                id: id
            }
        });
        ctx.apiSuccess(courses);
    }
}

module.exports = CoursesController;